const { SERVER_HOST, SERVER_POST } = require("../config/server");
const { create } = require("../service/file.service");
const userService = require("../service/user.service");

class FileController {
    async create(ctx, next) {
        console.log(ctx.request.file);
        // 1. 保存file信息
        const { filename, mimetype, size } = ctx.request.file
        // 1.1 上传的user
        const { id } = ctx.user;
        // 2. 将图片信息和id结合起来进行存储
        const result = await create(filename, mimetype, size, id);
        // 3.  上传成功后，将头像地址信息保存到user表中
        const avatarUrl = `${SERVER_HOST}:${SERVER_POST}/users/avatar/${id}`;
        const result2 = await userService.updateUserAvatar(avatarUrl, id)
        // 3.返回结果
        ctx.body = {
            code: 0,
            message: '头像上传成功～',
            data: avatarUrl
        }
    }
}

module.exports = new FileController()